﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.

using DWF.Studio.Custom;
using DWF.Studio.Host.RemoteHandler;
using DWF.Studio.RemoteCtrls;
using DWF.Studio.RemoteCtrls.Proxy;
using System;
using System.AddIn.Pipeline;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace DWF.Studio.Host
{
    [Serializable]
    internal class RemoteSearchHost : IDisposable
    {
        /// <summary>
        /// 设计器代理类
        /// </summary>
        private SearchExplorerProxy _searchProxy = null;
        /// <summary>
        /// 搜索栏
        /// </summary>
        private FrameworkElement _searchExplorer = null;
        /// <summary>
        /// 选中活动
        /// </summary>
        public GenericHandler<ShowSearchActivityEventArgs> selectionChanged = null;

        public RemoteSearchHost(SearchExplorerProxy searchProxy)
        {
            _searchProxy = searchProxy;

            selectionChanged = new GenericHandler<ShowSearchActivityEventArgs>();
            selectionChanged.SetAction(showSearch);
            _searchProxy._selectionChanged += selectionChanged.Action;
        }
        /// <summary>
        /// 获取查询结果
        /// </summary>
        /// <returns></returns>
        public FrameworkElement GetSearchExplorer()
        {
            if (_searchExplorer == null)
            {
                this._searchExplorer = FrameworkElementAdapters.ContractToViewAdapter(_searchProxy.GetSearchExplorer());
            }
            return _searchExplorer;
        }
        public void OpenSearchExplorer(List<string> listFile, double windowWidth)
        {
            _searchProxy.ShowSearchBox(listFile,windowWidth);
        }
        
        /// <summary>
        /// 打开活动
        /// </summary>
        /// <returns></returns>
        private void showSearch(ShowSearchActivityEventArgs e)
        {
            GlobalActionHandler.SelectionActivity?.Invoke(new TagEventArgs<string, string, string>(e.FullName, e.IdRef, e.DisplayName));
        }
        
        public void Dispose()
        {
            this._searchProxy?.Dispose();
            selectionChanged?.Dispose();
            this._searchProxy = null;
            selectionChanged = null;
        }
    }
}
